<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\Carousel;
use app\common\model\News;
use app\common\model\ProductCate;
use app\common\model\User;
use app\common\tool\Result;
use think\Response;

class Index extends Base
{
    public function index(): Response
    {
        $system = $this->system;
        $result = [
            'title' => $system['web_site_title'],
            'logo' => $system['web_site_logo'],
            'customer' => $system['customer'],
            'contact' => $system['contact']
        ];

        return Result::success('success', $result);
    }

    public function test(): Response
    {
        return Result::fail('访问无效');
    }

    public function communal($type = 'register_msg'): Response
    {
        $title = [
            'register_msg' => '注册协议',
            'privacy_msg' => '隐私协议',
            'company' => '公司介绍'
        ];
        if (empty($title[$type])) {
            return Result::fail('传参有误');
        }

        return Result::success('success', handleRichText($this->system[$type]));
    }

    public function carousel($position = ''): Response
    {
        $map = ['status' => 1];
        if (!empty($position)) {
            $map['position'] = $position;
        }
        $list = Carousel::field('type,title,images,video,link')->where($map)->order('sort desc,id asc')->select();

        return Result::success('success', $list);
    }

    public function inquiryUser($code, $type = 'code'): Response
    {
        $user = User::where([$type => $code])->field('id,code,nickname,mobile,photo')->find();
        if (!$user) {
            return Result::fail('会员不存在');
        }

        return Result::success('success', $user);
    }

    public function info(): Response
    {
        $result['carousel'] = Carousel::field('type,title,images,video,link')
            ->where(['status' => 1, 'position' => 1])->order('sort desc,id asc')->select();
        $result['news'] = News::field('id,title')->where(['status' => 1])
            ->order('sort desc,id asc')->limit(3)->select();
        $result['cate'] = ProductCate::getCate();

        return Result::success('success', $result);
    }
}